//
//  WeeklyLetterBean.swift
//  imprint
//
//  Created by 小柠檬🍋 on 2025/2/22.
//

import Foundation
import GRDB

// 聊天
class ChatBean : IDbTable, ChatBaseView {
    
    static let databaseTableName = "Chat"
    
    //db排除字段
    func doCustomDBExcluded()  -> [String? : Any?] {
        return doExcluded(subject: self, excluded: ["targetChat","select"])
    }
    
    var objectId: String?
    var type: Int = IChatType.USER.rawValue
    var contentType : Int = IChatContentType.TYPE_CONTENT.rawValue
    var role: RoleBean? = nil
    var content: String? = nil
    var time: Int64 = 0
    var aiUserId: String? = nil
    var userId: String? = nil
    var createTime: Int64 = 0//创建时间
    var updateTime: Int64 = 0//更新时间时间
    var tokens: Int64 = 0
    var modifySign: Bool = false//修改标识
    var status: Int = IStatusType.NORMAL.rawValue
    
    var voiceHex: String? = nil//语音hex,不能使用aiRole里面的，因为里面是存储默认的
    var voicePath: String? = nil//语音路径
    var extra: String? = nil//额外数据
    var read: Bool? = nil//是否已读
    var memory: Bool? = nil//是否提取记忆
    
    //临时数据
    var targetChat: ChatBean? = nil//引用内容
    var select: Bool? = nil//选中

    
    func getSound() -> SoundBean? {
        if (IChatContentType.TYPE_VOICE_MESSAGE.rawValue == contentType) {
            guard let sound = try? fromJson(extra, SoundBean.self) else {
                return nil
            }
            return sound
        }
        return nil
    }
    
    
    //引用显示文本
    func getTargetChatStr()-> String {
        switch (contentType) {
        case IChatContentType.TYPE_EMOJI_PICTURE.rawValue:
            return (role?.name ?? "") + "："
        case IChatContentType.TYPE_PICTURE.rawValue:
            return (role?.name ?? "") + "："
        default:
            return (role?.name ?? "") + "：" + getShowContentStr()
        }
    }
    
    func getShowContentStr() -> String {
        var text = content ??  "暂无消息"
        if text.isEmpty {
            text = "暂无消息"
        }
        let contentType = contentType
        switch (contentType) {
        case IChatContentType.TYPE_PICTURE.rawValue:
            return "[图片]"
        case IChatContentType.TYPE_EMOJI_PICTURE.rawValue:
            return "[表情]"
        case IChatContentType.TYPE_CONTENT.rawValue:
            return text
        case IChatContentType.TYPE_VOICE_MESSAGE.rawValue:
            return "[语音]"
        case IChatContentType.TYPE_ACTION.rawValue:
            return getPatStr()
        default:
            if text.isEmpty {
                text = "暂无消息"
            }
            return text
        }
    }
    
    //拍一拍文本
    func getPatStr()-> String {
        guard let pat = try? fromJson(extra, ChatMessageBean.Pat.self) else {
            //类型转换失败
            return ""
        }
        let startName = if (role?.roleId == userManager.getUserId()) {
            "我 "
        } else {
            "\"\(role?.name ?? "")\" "
        }
        if ((pat.content ?? "")?.isEmpty == true) {
            let endName = if (pat.userId == userManager.getUserId()) {
                "我"
            } else {
                "\"\(pat.userName ?? "")\""
            }
            return "\(startName)\(pat.action ?? "")\(endName)\(pat.suffix ?? "")"
        } else {
            return "\(startName)\(pat.content ?? "")"
        }
    }
    
    func getTargetPicture()-> EmojiPictureBean? {
        switch (contentType) {
        case IChatContentType.TYPE_EMOJI_PICTURE.rawValue:
            return try? fromJson(extra ?? "", EmojiPictureBean.self)
        case IChatContentType.TYPE_PICTURE.rawValue:
            return try? fromJson(extra ?? "", EmojiPictureBean.self)
        default:
            return nil
        }
    }
}

extension ChatBean {
    // db数据库指定字段
    public func encode(to container: inout PersistenceContainer) {
        doDBExcluded(to: &container)
    }
}
